﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using Vectrosity;

public class vectrocityLine : MonoBehaviour {
	
	public Material lineMaterial;
	public int maxPoints = 1000;
	public float lineWidth = 4.0f;
	public float minPixelMove = 5;	// Must move at least this many pixels per sample for a new segment to be recorded
	public float timeBeforeFade = 0.5f;
	
	//Queue<timedPoints> pointQueue;
	//Queue<timedPoints> partnerPointQueue;
	
	//private Vector2[] linePoints;
	//private Color[] lineColors;
	private CVectrocityLine currentLine;
	private int lineIndex = 0;
	private Vector3 previousPosition;
	private float sqrMinPixelMove;
	private bool canDraw = true;
	public List<CVectrocityLine> listOfLines;
	Camera mainCam;

	bool firstPress = true;
	
	void Start () 
	{
		listOfLines = new List<CVectrocityLine>();
		//linePoints = new Vector2[maxPoints];
		//lineColors = new Color[maxPoints - 1];
		//line = new VectorLine("DrawnLine", linePoints, lineMaterial, lineWidth, LineType.Continuous, Joins.Weld);
		sqrMinPixelMove = minPixelMove * minPixelMove;
		mainCam = GameObject.FindGameObjectWithTag("MainCamera").camera;
		GameObject tmpObj;
		if((tmpObj = GameObject.Find("LineManager")) != null)
		{
			currentLine = new CVectrocityLine(maxPoints, lineMaterial, lineWidth, LineType.Continuous, Joins.Weld,this);
			currentLine.addPoint(new Vector3(100,100,100));
			Destroy(tmpObj);
			VectorLine.Destroy(ref currentLine.myLine);
		}
	}
	
	void Update () 
	{
		if(networkView.isMine)
		{
			Vector2 mousePos = Input.mousePosition;
			Vector3 tmpPoint = mainCam.ScreenToWorldPoint(new Vector3(mousePos.x, mousePos.y, 2.0f));
			if (Input.GetMouseButtonDown(0)) 
			{
				currentLine = new CVectrocityLine(maxPoints, lineMaterial, lineWidth, LineType.Continuous, Joins.Weld,this);

				listOfLines.Add(currentLine);
				previousPosition = Vector3.zero;
				previousPosition.z = 1000;
				lineIndex = 0;
				//currentLine.addPoint(tmpPoint);
				canDraw = true;
				
				networkView.RPC("DrawNewLine",RPCMode.Others,tmpPoint);
			}
			else if (Input.GetMouseButton(0) && (tmpPoint - previousPosition).sqrMagnitude > sqrMinPixelMove && canDraw) 
			{
				previousPosition = mousePos;
				currentLine.addPoint(tmpPoint);
				lineIndex++;
				//Debug.Log("THIS PT :" + tmpPoint);
				if (lineIndex >= maxPoints - 3)
					canDraw = false;
				networkView.RPC("AddAPoint",RPCMode.Others,tmpPoint);
				//line.Draw();
			}
			else if (Input.GetMouseButtonUp(0))
			{
				currentLine.endPoint();
				currentLine = null;
				networkView.RPC("EndPoint",RPCMode.Others);
			}
		}
		
		for(int i = 0; i < listOfLines.Count; i++)
		{
			listOfLines[i].Update();
		}
	}
	
	[RPC]
	void DrawNewLine(Vector3 tmpPoint)
	{
		currentLine = new CVectrocityLine(maxPoints, lineMaterial, lineWidth, LineType.Continuous, Joins.Weld,this);
		listOfLines.Add(currentLine);
		lineIndex = 0;	
		//currentLine.addPoint(tmpPoint);
	}
	
	[RPC]
	void AddAPoint(Vector3 tmpPoint)
	{
		if (lineIndex >= maxPoints - 3)
			return;
		currentLine.addPoint(tmpPoint);
		lineIndex++;
	}
	
	[RPC]
	void EndPoint()
	{
		currentLine.endPoint();
		currentLine = null;
	}

}
